<?php
namespace app\api\command;

use think\console\Command;
use think\console\Input;
use think\console\Output;
use app\Response;
use QL\QueryList;
use \Curl\Curl;
use \Curl\MultiCurl;
use think\Db;

class kj extends Command {
	const HEADER = [
			        //填写从浏览器获取到的cookie
			        'User-Agent' => 'Mozilla/5.0 (Windows NT 10.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.108 Safari/537.36 WangPai 2345Explorer/8.4.1.14998',
			    ];
	protected function configure()  
	{  
	        $this->setName('test')->setDescription('Command Test');  
	}  
	  
	protected function execute(Input $input, Output $output)  
	{  
		//$result = $this->getDownLink();
		$result = $this->book(6);
		if(is_array($result)){
			foreach ($result as $key => $val) {
				$key = self::encoding($key, "GBK", "UTF-8");
				$val = self::encoding($val, "GBK", "UTF-8");
				$result[$key] = $val;
			}
		}else{
			$result = self::encoding($result, "GBK", "UTF-8");
		}
		
	          	$output->writeln($result);  
	}  
	/**
	 * [book 整个教材分步录入数据]
	 * @param  integer $step    [步骤]
	 * @param  boolean $substep [是否开启分步录入，默认true]
	 * @return [type]           [description]
	 */
	public function book($step=0,$substep=true){
		//$subject = Db::connect("db_ppt")->name("ppt_subject")->select();
		$result = '完成0步';
		$book_link = array(
			'1' => 'http://www.kjzhan.com/yuwen/',              //语文,subject_id=1
			'2' => 'http://www.kjzhan.com/shuxue/', 	//数学,subject_id=2
			'3' => 'http://www.kjzhan.com/yingyu/', 	//英语,subject_id=3
			'8' => 'http://www.kjzhan.com/wuli/', 		//物理,subject_id=8
			'9' => 'http://www.kjzhan.com/huaxue/', 	//化学,subject_id=9
			'7' => 'http://www.kjzhan.com/shengwu/', 	//生物,subject_id=7
			'6' => 'http://www.kjzhan.com/dili/', 		//地理,subject_id=6
			'5' => 'http://www.kjzhan.com/lishi/', 		//历史,subject_id=5
			'4' => 'http://www.kjzhan.com/zhengzhi/', 	//政治,subject_id=4
			);
		//录入课本信息，检测年级或选修、必修信息是否存在，不存在则新增
		if($step<=1)
		{
			$step_one = self::getGrade($book_link);
			$result = "步骤1结束:课本录入完成";
			if($substep){
				//分步完成，结束
				return $result;
				exit;
			}
		}
		//获取章节信息列表
		if($step<=2)
		{
			$result = $this->getChapter();
			if($substep){
				//分步完成，结束
				return $result;
				exit;
			}
		}
		//获取课件下载页的html地址
		if($step<=3)
		{
			$result = $this->getCourseware();
			if($substep){
				//分步完成，结束
				return $result;
				exit;
			}
		}
		//更新章节资源数量
		if($step<=4)
		{
			$result = $this->countChapterResource();
			if($substep){
				//分步完成，结束
				return $result;
				exit;
			}
		}
		//更新课本资源数量		
		if($step<=5)
		{
			$result = $this->countBookResource();
			if($substep){
				//分步完成，结束
				return $result;
				exit;
			}
		}
		//由下载页html页获取并更新真实下载地址
		if($step<=6)
		{
			$result = $this->getDownLink();
			if($substep){
				//分步完成，结束
				return $result;
				exit;
			}
		}
		return $result;
	}
	/**
	 * [getRealLink 获取真实的下载地址]
	 * @param  string $url           [下载中转业地址]
	 * @param  string $courseware_id [文件ID]
	 * @return [type]                [description]
	 */
	public function getRealLink($url='',$courseware_id=''){
		$result = false;
		if($url && $courseware_id){
			//$url = 'http://www.kjzhan.com/plus/download.php?open=0&aid=22448&cid=3';
			$url_Arr = explode('?', $url);
			$new_url = isset($url_Arr[0])?$url_Arr[0]:'';
			$condition = isset($url_Arr[1])?$url_Arr[1]:'';
			$params = array();
			if($condition){
				$condition_Arr = explode('&', $condition);
				if($condition_Arr){
					foreach ($condition_Arr as $m => $o) {
						$param = explode('=', $o);
						$key = isset($param[0])?$param[0]:'';
						$value = isset($param[1])?$param[1]:'';
						if($key){
							$params[$key] = $value;
						}
					}
				}
			}
			
			if($params && $new_url){
				$rules = array(
					'link' => array('a','href')
					);
				$ql = QueryList::get($new_url,$params,[
				    'headers' => [
				        //填写从浏览器获取到的cookie
				        'User-Agent' => self::HEADER,
				    ]
				]);
				$html = $ql->getHtml();
				// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
				$data = QueryList::html($html)->rules($rules)->query()->getData();
				//打印结果
				$uhash_href = $data->all();
				
				if($uhash_href){
					foreach ($uhash_href as $k => $v) {
						$uhash = isset($v['link'])?$v['link']:'';
						if(stristr($uhash, 'uhash')){
							//获取带uhash的下载中转页
							$down = (stristr($uhash,'www'))?$uhash:('http://www.kjzhan.com'.$uhash);
							$curl = new Curl();
							$curl->setRetry(3);
							$curl->get($down);
							if ($curl->error) {
							    echo 'Error: ' . $curl->errorCode . ': ' . $curl->errorMessage . "\n";
							    echo 'retries: ' . $curl->retries . "\n";
							} 

							$resHeader = $curl->responseHeaders;
							//真实下载地址
							$real_link = isset($resHeader['location'])?$resHeader['location']:'';
							if($real_link){
								$real_link = (stristr($real_link,'www'))?$real_link:('http://www.kjzhan.com'.$real_link);
								//将真实地址录入数据库
								$update = Db::connect("db_ppt")->name("tbl_courseware")->where('courseware_id', $courseware_id)->update(['link' => $real_link, 'update_time' => date('Y-m-d H:i:s')]);
								if($update){
									$result = $update;
								}
							}							
						}
					}
				}
				//释放资源
				$ql->destruct();				
			}
		}
		return $result;	
	}
	/**
	 * [getDownLink 由下载页html页获取并更新真实下载地址]
	 * @return [type] [description]
	 */
	public function getDownLink(){
		set_time_limit(0);
		//获取资源下载链接
		$courseware = Db::connect("db_ppt")->name("tbl_courseware")->where('link', null)->select();
		$rule1 = array(
			'link' => array('a','href')
			);
		$total = 0;
		if($courseware){
			foreach ($courseware as $key => $val) {
				$href = isset($val['html'])?$val['html']:'';
				$courseware_id = isset($val['courseware_id'])?$val['courseware_id']:'';
				if($href && $courseware_id){
					$ql = QueryList::get($href,[
						],[
					    'headers' => self::HEADER
					]);
					$html = $ql->getHtml();
					// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
					$data = QueryList::html($html)->range('td>strong')->rules($rule1)->query()->getData();
					
					//打印结果
					$info = $data->all();
					if($info){
						foreach ($info as $k => $v) {
							$down = isset($v['link'])?$v['link']:'';
							if($down && stristr($down, 'download')){
								//含有download.php才是下载中转页面
								$down = (stristr($down,'www'))?$down:('http://www.kjzhan.com'.$down);
								//获取真实的下载地址
								$real_link = self::getRealLink($down,$courseware_id);
								if($real_link){
									$total++;
								}
							}
						}
					}
					//释放资源
					$ql->destruct();
				}
			}
		}
		$result = array('本次更新资源真实下载地址'=>"{$total}条");
		return $result;
		//Response::show(200,'ok',$result);
	}
	/**
	 * [getCourseware 获取课件下载页的html地址]
	 * @return [type] [description]
	 */
	public function getCourseware(){
		set_time_limit(0);
		//获取未录入课件的章节信息
		$chapter_info = Db::connect("db_ppt")->name("tbl_chapter")->where('courseware_over', 0)->select();
		$rule1 = array(
			'link' => array('a','href'),
			'name' => array('a','text')
			);
		$rule2 = array(
			'link' => array('option','value')
			);
		$total = 0;
		$chapter_total = 0;
		if($chapter_info){
			foreach ($chapter_info as $key => $val) {
				$link = isset($val['link'])?$val['link']:'';
				$book_id = isset($val['book_id'])?$val['book_id']:'';
				$chapter_id = isset($val['chapter_id'])?$val['chapter_id']:'';
				if($link){
					//获取第一页，判断是否是多页资源
					$ql = QueryList::get($link,[
						],[
					    'headers' => self::HEADER
					]);
					$html = $ql->getHtml();
					// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据

					$data = $ql->range('$(".pages")')->find('option')->attrs('value');
					//打印结果
					$info = $data->all();
					$pages = array();
					if($info){
						foreach ($info as $k => $v) {
							$link = trim($link);
							$len = strlen($link)-1;
							//判断link最后一位是否是分隔符
							if($link[$len] != '/'){
								$pages[] = $link.'/'.$v;
							}else{
								$pages[] = $link.$v;
							}
							
						}
					}else{
						//若为单页
						$pages[] = $link;
					}
					if($pages){
						//按页获取资源
						foreach ($pages as $m => $o) {
							$ql = QueryList::get($o,[
								],[
							    'headers' => self::HEADER
							]);
							$htm = $ql->getHtml();
							$data = QueryList::html($htm)->range('$(".gamelist_pcgame")>li>div')->rules($rule1)->query()->getData();
							$page_data = array();
							//获取资源页面信息
							if($data){
								foreach ($data as $m => $o) {
									$href = isset($o['link'])?$o['link']:'';
									$href = (stristr($href,'www'))?$href:('http://www.kjzhan.com'.$href);
									$name = isset($o['name'])?$o['name']:'';
									$name = !empty($name)?self::queryListEncodeing($name):'';
									$page_data[] = array('link'=>$href, 'name'=>$name);
									//将资源业的html地址存入tbl_courseware表
									if($href && $name){
										//验证html页面链接是否已存入数据表
										$is_html_save = Db::connect("db_ppt")->name("tbl_courseware")->where('html', $href)->find();
										if(!$is_html_save){
											$courseware_html_data = array(
												'name' => $name,
												'book_id' => $book_id,
												'chapter_id' => $chapter_id,
												'html' => $href,
												'create_time' => date('Y-m-d H:i:s'),
												'update_time' => date('Y-m-d H:i:s')
												);
											$courseware_html_insert = Db::connect("db_ppt")->name("tbl_courseware")->insertGetId($courseware_html_data);
											if($courseware_html_insert){
												$total++;
											}
										}
									}									
								}
							}

						}
					}
				}
				//每本课本页面链接录入完成后，修改课本tbl_chapter表中courseware_over课件信息是否获取完成的状态
				$update_chapter = Db::connect("db_ppt")->name("tbl_chapter")->where('chapter_id', $chapter_id)->update(['courseware_over' => 1, 'update_time' => date('Y-m-d H:i:s')]);
				if($update_chapter){
					$chapter_total++;
				}
			}
		}
		$result = array('total'=>"本次录入课件下载页{$total}条数据",'chapter_total'=>"本次完成{$chapter_total}章节的录入");
		return $result;
		//Response::show(200,'ok',$result);
	}
	/**
	 * [countChapterResource 更新章节资源数量]
	 * @param  string $chapter_id [章节ID]
	 * @return [type]             [description]
	 */
	public  function countChapterResource($chapter_id=''){
		$courseware = array();
		$total = 0;
		if($chapter_id){
			$num = Db::connect("db_ppt")->name("tbl_courseware")->where('chapter_id', $chapter_id)->count();
			$courseware[] = array('chapter_id'=>$chapter_id, 'num'=>$num);
		}else{
			$courseware = Db::connect("db_ppt")->name("tbl_courseware")->field('chapter_id,count(*) as num')->group('chapter_id')->select();
		}
		if($courseware){
			foreach ($courseware as $key => $val) {
				$chapter_id = isset($val['chapter_id'])?$val['chapter_id']:0;
				$num = isset($val['num'])?$val['num']:0;
				//更新tbl_chapter表中的resource_num章节资源数
				if($chapter_id){
					$update_chapter = Db::connect("db_ppt")->name("tbl_chapter")->where('chapter_id', $chapter_id)->update(['resource_num' => $num, 'update_time' => date('Y-m-d H:i:s')]);
					if($update_chapter){
						$total++;
					}
				}
			}
		}
		//return $total;
		$result = array('本次更新章节'=>$total.'条');
		return $result;
		//Response::show(200,'ok',$result);
	}
	/**
	 * [countBookResource 按课本统计资源数量]
	 * @param  string $book_id [课本ID]
	 * @return [type]          [description]
	 */
	public function countBookResource($book_id=''){
		$courseware = array();
		$total = 0;
		if($book_id){
			$num = Db::connect("db_ppt")->name("tbl_courseware")->where('book_id', $book_id)->count();
			$courseware[] = array('book_id'=>$book_id, 'num'=>$num);
		}else{
			$courseware = Db::connect("db_ppt")->name("tbl_courseware")->field('book_id,count(*) as num')->group('book_id')->select();
		}
		if($courseware){
			foreach ($courseware as $key => $val) {
				$book_id = isset($val['book_id'])?$val['book_id']:0;
				$num = isset($val['num'])?$val['num']:0;
				//更新tbl_book表中的resource_num章节资源数
				if($book_id){
					$update = Db::connect("db_ppt")->name("tbl_book")->where('book_id', $book_id)->update(['resource_num' => $num, 'update_time' => date('Y-m-d H:i:s')]);
					if($update){
						$total++;
					}
				}
			}
		}
		//return $total;
		$result = array('本次更新课本'=>$total.'条');
		return $result;
		//Response::show(200,'ok',$result);
	}

	/**
	 * [getChapter 获取章节信息列表]
	 * @return [type] [description]
	 */
	public function getChapter(){
		//获取课本信息
		$book_info = Db::connect("db_ppt")->name("tbl_book")->where('chapter_over', 0)->select();
		$rule1 = array(
			'link' => array('a','href'),
			'name' => array('a','text')
			);
		$book_total = 0;
		$chapter_total = 0;
		if($book_info){
			foreach ($book_info as $key => $val) {
				$link = isset($val['link'])?$val['link']:'';
				$book_id = isset($val['book_id'])?$val['book_id']:'';
				if($link){
					$ql = QueryList::get($link,[
						],[
					    'headers' => self::HEADER
					]);
					$html = $ql->getHtml();
					// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
					$data = QueryList::html($html)->range('$(".style1")>li')->rules($rule1)->query()->getData();
					//打印结果
					$info = $data->all();
					$chapter  = array();
					if($info){
						foreach ($info as $k => $v) {
							$href = isset($v['link'])?$v['link']:'';
							$name = isset($v['name'])?$v['name']:'';
							$name = !empty($name)?self::queryListEncodeing($name):'';
							if($href && $name){
								$href = (stristr($href,'www'))?$href:('http://www.kjzhan.com'.$href);
								$name = str_replace('"', '', $name);
								$name = trim($name);
								$chapter[] = array('link'=>$href, 'name'=>$name);
								//检验章节是否已录入数据库,未录入的新增
								$is_chapter_save = Db::connect("db_ppt")->name("tbl_chapter")->where('link', $href)->find();
								if(!$is_chapter_save){
									$chapter_data = array(
										'name' => $name,
										'book_id' => $book_id,
										'link'  => $href,
										'create_time' => date('Y-m-d H:i:s'),
										'update_time' => date('Y-m-d H:i:s')
										);
									$chapter_insert = Db::connect("db_ppt")->name("tbl_chapter")->insertGetId($chapter_data);
									if($chapter_insert){
										$chapter_total++;
									}
								}
							}
						}
						//章节信息录入完成后修改tbl_book中的chapter_over章节完成字段
						$update_book = Db::connect("db_ppt")->name("tbl_book")->where('book_id', $book_id)->update(['chapter_over' => 1]);
						if($update_book){
							$book_total++;
						}
					}				
				}
			}
		}
		$result = array('book_total'=>('本次录入课本'.$book_total.'本'), 'chapter_total'=>('本次录入'.$chapter_total.'章节'));
		return $result;
		//Response::show(200,'ok',$result);
	}

	/**
	 * [getGrade 获取年级，选修和必修信息,课本信息]
	 * @param  [array] $book_link [学科连接信息]
	 * @return [type]            [description]
	 * $book_link = array(
			'1' => 'http://www.kjzhan.com/yuwen/'      //语文,subject_id=1
			);
	 */
	public static function getGrade($book_link){
		$rule1 = array(
			'link' => array('a','href'),
			'name' => array('a','text')
			);
		if($book_link){
			//查出已存在的年级信息，高中只有选修和必修
			$grade_info = Db::connect("db_ppt")->name("ppt_grade")->select();
			$had_grade = array();
			$grade_Arr = array();
			if($grade_info){
				foreach ($grade_info as $key => $val) {
					$grade_id = isset($val['grade_id'])?$val['grade_id']:'';
					$grade = isset($val['grade'])?$val['grade']:'';
					if($grade_id && $grade && !in_array($grade, $had_grade)){
						$had_grade[] = $grade;
						//因为array_flip反转数组键名与键值只适用于一维数组，所以此处只能用循环重组，
						//为了下面方便取grade_id值，将grade_info的键名与键值反转
						$grade_Arr[$grade] = $grade_id;
					}
				}
			}

			foreach ($book_link as $key => $val) {
				$ql = QueryList::get($val,[
					],[
				    'headers' => self::HEADER
				]);
				$html = $ql->getHtml();
				// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
				$data = QueryList::html($html)->range('[align="left"]>strong')->rules($rule1)->query()->getData();
				//打印结果
				$info = $data->all();
				$book = array();
				$subject_id = $key;
				if($info){
					foreach ($info as $k => $v) {
						$link = isset($v['link'])?$v['link']:'';
						$name = isset($v['name'])?$v['name']:'';
						$name = !empty($name)?self::queryListEncodeing($name):'';
						if($link && $name){
							$name = str_replace('"', '', $name);
							$name = trim($name);
							$link = (stristr($link,'www'))?$link:('http://www.kjzhan.com'.$link);
							$book[] = array('link'=>$link,'name'=>$name);
							$grade_data = array('grade'=>$name);
							if(!in_array($name, $had_grade)){
								//若年级或选修、必修信息不存在，则新增
								$grade_id = Db::connect("db_ppt")->name("ppt_grade")->insertGetId($grade_data);
								$had_grade[] = $name;
								$grade_Arr[$name] = $grade_id;
							}else{
								//若年级或选修、必修信息存在
								$grade_id = $grade_Arr[$name];
							}

							if($grade_id && $link){
								//查询grade_url中链接信息是否已存在,不存在则新增
								$is_save = Db::connect("db_ppt")->name("grade_url")->where('url', $link)->find();
								if(!$is_save){
									$grade_url_data = array('grade'=>$name, 'url'=>$link, 'grade_id'=>$grade_id, 'semester'=>3);
									$grade_url = Db::connect("db_ppt")->name("grade_url")->insertGetId($grade_url_data);
								}else{
									$grade_url = isset($is_save['grade_url'])?$is_save['grade_url']:0;
								}
								if($grade_url){
									//查询grade_subject中链接信息是否已存在,不存在则新增
									$grade_subject = Db::connect("db_ppt")->name("grade_subject")->where('url', $link)->find();
									if(!$grade_subject){
										$grade_subject_data = array(
											'grade_url'=>$grade_url, 
											'subject_id'=>$subject_id, 
											'semester' => 3, 
											'url'=>$link
											);
										$subject = Db::connect("db_ppt")->name("grade_subject")->insertGetId($grade_subject_data);
									}
								}
								//验证tbl_book中数据是否存在，不存在则新增‘
								$is_book_save = Db::connect("db_ppt")->name("tbl_book")->where('link', $link)->find();
								if(!$is_book_save){
									$subject = Db::connect("db_ppt")->name("ppt_subject")->where('subject_id', $subject_id)->find();
									$subject_name = isset($subject['subject'])?$subject['subject']:'';

									$book_name = '高中'.$subject_name.$name;
									$book_data = array(
										'name'=>$book_name, 
										'subject_id'=>$subject_id,
										'grade_id'=>$grade_id, 
										'semester'=>3,
										'link'=>$link,
										'source'=>'www.kjzhan.com',
										'create_time'=> date('Y-m-d H:i:s'),
										'update_time'=>date('Y-m-d H:i:s')
										);
									$book_save = Db::connect("db_ppt")->name("tbl_book")->insertGetId($book_data);
								}
							}
						}
					}
				}			
			}
		}else{
			return false;
		}
		return true;
	}

	public function getLink(){
		$url = 'http://www.kjzhan.com/plus/download.php';
		
		$cookie = '__cfduid=d5421a818c526dbd1d1dc6c38b73496851508221253; UM_distinctid=15f28fcad151-0505879e6-724d7c42-1aeaa0-15f28fcad170; yjs_id=TW96aWxsYS81LjAgKFdpbmRvd3MgTlQgMTAuMCkgQXBwbGVXZWJLaXQvNTM3LjM2IChLSFRNTCwgbGlrZSBHZWNrbykgQ2hyb21lLzQ3LjAuMjUyNi4xMDggU2FmYXJpLzUzNy4zNiBXYW5nUGFpIDIzNDVFeHBsb3Jlci84LjQuMS4xNDk5OHx3d3cua2p6aGFuLmNvbXwxNTA4MjIxMTY5NTUwfA; ctrl_time=1; CNZZDATA5695801=cnzz_eid%3D1409611725-1508217916-%26ntime%3D1508813199; Hm_lvt_9ef6f89846c18bd6f03ec8b0068d9c8e=1508221170,1508229432,1508808094; Hm_lpvt_9ef6f89846c18bd6f03ec8b0068d9c8e=1508813516';
		
		$rules = array(
			'link' => array('a','href')
			);
		$ql = QueryList::get($url,[
			'open' => 0,
			'aid'    => 22448,
			'cid'    => 3
			],[
		    'headers' => [
		        //填写从浏览器获取到的cookie
		        'User-Agent' => self::HEADER,
		        'Cookie'    => $cookie
		    ]
		]);
		$html = $ql->getHtml();
		// 过程:设置HTML=>设置采集规则=>执行采集=>获取采集结果数据
		$data = QueryList::html($html)->rules($rules)->query()->getData();
		//打印结果
		$uhash_href = $data->all();
		if($uhash_href){
			foreach ($uhash_href as $k => $v) {
				$uhash = isset($v['link'])?$v['link']:'';
				if(stristr($uhash, 'uhash')){
					$down = (stristr($uhash,'www'))?$uhash:('http://www.kjzhan.com'.$uhash);
				}
			}
		}
		var_dump($down);
	}

	/**
	 * [queryListEncodeing phpQuery认不清目标网页是什么编码，即找不到meta属性的时候，会默认转换成iso-8859-1的格式,所以乱码么，需要先从iso-8859-1的格式转换成utf-8的格式，再转换成GBK的格式]
	 * @param  string $str [需要转化的内容]
	 * @return [type]      [description]
	 */
	public static function queryListEncodeing($str=''){
		if($str){
			$str = mb_convert_encoding($str,'ISO-8859-1','UTF-8');
			$str = mb_convert_encoding($str,'UTF-8','GBK');
		}
		return $str;
	}
	/**
	 * [encoding 编码转化]
	 * @param  string $str           [要转化的字符]
	 * @param  string $encoding_to   [要转化的目的编码]
	 * @param  string $encoding_from [原编码]
	 * @return [type]                [description]
	 */
	public static function encoding($str='', $encoding_to="UTF-8", $encoding_from="GBK"){
		if($str){
			//已知原编码为encoding_from，转换为encoding_to
			$str = mb_convert_encoding($str, $encoding_to, $encoding_from); 
		}
		return $str;
	}
}